<template>
  <div class="member-dashboard">
    <!-- 顶部会员卡片 -->
    <el-card class="member-card">
      <div class="member-header">
        <div class="member-avatar">
          <el-avatar :size="100" :src="memberInfo.avatar">
            <el-icon :size="50"><User /></el-icon>
          </el-avatar>
          <el-tag :type="getLevelTag(memberInfo.level)" class="level-badge">
            {{ getLevelName(memberInfo.level) }}
          </el-tag>
        </div>
        <div class="member-basic">
          <h2 class="member-name">{{ memberInfo.name }}</h2>
          <div class="member-meta">
            <span class="meta-item">
              <el-icon><Phone /></el-icon>
              {{ memberInfo.phone }}
            </span>
            <span class="meta-item">
              <el-icon><Message /></el-icon>
              {{ memberInfo.email }}
            </span>
            <span class="meta-item">
              <el-icon><Calendar /></el-icon>
              生日: {{ memberInfo.birthday }}
            </span>
            <span class="meta-item">
              <el-icon><Clock /></el-icon>
              注册: {{ memberInfo.registerTime }}
            </span>
          </div>
          <div class="member-tags">
            <el-tag v-for="tag in memberInfo.tags" :key="tag" size="small" style="margin-right: 8px;">
              {{ tag }}
            </el-tag>
          </div>
        </div>
        <div class="member-actions">
          <el-button type="primary" @click="handleRecharge">
            <el-icon><Wallet /></el-icon>
            充值
          </el-button>
          <el-button type="success" @click="handleGivePoints">
            <el-icon><Star /></el-icon>
            赠送积分
          </el-button>
          <el-button type="warning" @click="handleSendCoupon">
            <el-icon><Ticket /></el-icon>
            发券
          </el-button>
          <el-dropdown>
            <el-button>
              更多操作
              <el-icon class="el-icon--right"><ArrowDown /></el-icon>
            </el-button>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item @click="handleEdit">编辑资料</el-dropdown-item>
                <el-dropdown-item @click="handleUpgrade">升级等级</el-dropdown-item>
                <el-dropdown-item @click="handleAddTag">添加标签</el-dropdown-item>
                <el-dropdown-item divided @click="handleFreeze">冻结账户</el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </div>
      </div>
    </el-card>

    <!-- 核心数据指标 -->
    <el-row :gutter="20" class="kpi-row">
      <el-col :xs="12" :sm="6" v-for="kpi in kpiData" :key="kpi.id">
        <el-card class="kpi-card" :class="'kpi-' + kpi.id">
          <div class="kpi-icon" :style="{ background: kpi.color }">
            <el-icon :size="32">
              <component :is="kpi.icon" />
            </el-icon>
          </div>
          <div class="kpi-content">
            <div class="kpi-label">{{ kpi.label }}</div>
            <div class="kpi-value">{{ kpi.value }}</div>
            <div class="kpi-desc">{{ kpi.desc }}</div>
          </div>
        </el-card>
      </el-col>
    </el-row>

    <!-- 数据分析 -->
    <el-row :gutter="20">
      <!-- 消费分析 -->
      <el-col :xs="24" :lg="12">
        <el-card>
          <template #header>
            <div class="card-header">
              <span>📈 消费趋势分析</span>
              <el-radio-group v-model="consumptionPeriod" size="small">
                <el-radio-button value="week">7天</el-radio-button>
                <el-radio-button value="month">30天</el-radio-button>
                <el-radio-button value="year">全年</el-radio-button>
              </el-radio-group>
            </div>
          </template>
          <div class="chart-container" id="consumptionChart">
            <div class="chart-placeholder">消费趋势图表</div>
          </div>
          <div class="consumption-stats">
            <el-row :gutter="20">
              <el-col :span="8">
                <div class="stat-item">
                  <div class="stat-label">最高单次</div>
                  <div class="stat-value">¥{{ maxOrder.toLocaleString() }}</div>
                </div>
              </el-col>
              <el-col :span="8">
                <div class="stat-item">
                  <div class="stat-label">平均消费</div>
                  <div class="stat-value">¥{{ avgOrder.toLocaleString() }}</div>
                </div>
              </el-col>
              <el-col :span="8">
                <div class="stat-item">
                  <div class="stat-label">消费频次</div>
                  <div class="stat-value">{{ frequency }}次/月</div>
                </div>
              </el-col>
            </el-row>
          </div>
        </el-card>
      </el-col>

      <!-- 会员成长 -->
      <el-col :xs="24" :lg="12">
        <el-card>
          <template #header>
            <span>🌟 会员成长</span>
          </template>
          <div class="growth-info">
            <div class="current-level">
              <span class="level-label">当前等级</span>
              <el-tag :type="getLevelTag(memberInfo.level)" size="large">
                {{ getLevelName(memberInfo.level) }}
              </el-tag>
            </div>
            <div class="growth-progress">
              <div class="progress-header">
                <span>成长值进度</span>
                <span class="progress-value">{{ memberInfo.growth }} / {{ nextLevelGrowth }}</span>
              </div>
              <el-progress 
                :percentage="growthPercentage" 
                :color="customColors"
                :stroke-width="20"
              >
                <template #default="{ percentage }">
                  <span class="percentage-label">{{ percentage }}%</span>
                </template>
              </el-progress>
              <div class="progress-tip">
                还需 <span class="highlight">{{ nextLevelGrowth - memberInfo.growth }}</span> 成长值升级到 
                <span class="highlight">{{ nextLevelName }}</span>
              </div>
            </div>
            <div class="growth-actions">
              <el-button type="primary" @click="handleViewGrowthRules">
                查看成长规则
              </el-button>
            </div>
          </div>

          <el-divider />

          <div class="rights-info">
            <h4>当前权益</h4>
            <el-row :gutter="10">
              <el-col :span="12" v-for="right in currentRights" :key="right.id">
                <div class="right-item">
                  <el-icon :size="20" :color="right.color">
                    <component :is="right.icon" />
                  </el-icon>
                  <span>{{ right.name }}</span>
                  <el-icon class="check-icon" color="#67c23a"><Select /></el-icon>
                </div>
              </el-col>
            </el-row>
          </div>
        </el-card>
      </el-col>
    </el-row>

    <!-- 财务信息 -->
    <el-row :gutter="20" style="margin-top: 20px;">
      <el-col :xs="24" :lg="12">
        <el-card>
          <template #header>
            <div class="card-header">
              <span>💰 财务总览</span>
              <el-button size="small" @click="handleViewFinance">详细账单</el-button>
            </div>
          </template>
          <div class="finance-overview">
            <div class="finance-item">
              <div class="finance-icon balance">
                <el-icon :size="32"><Wallet /></el-icon>
              </div>
              <div class="finance-content">
                <div class="finance-label">储值余额</div>
                <div class="finance-value">¥{{ memberInfo.balance.toFixed(2) }}</div>
                <div class="finance-action">
                  <el-button link type="primary" @click="handleRecharge">充值</el-button>
                  <el-button link type="warning" @click="handleAdjustBalance">调整</el-button>
                </div>
              </div>
            </div>
            <el-divider />
            <div class="finance-item">
              <div class="finance-icon points">
                <el-icon :size="32"><Star /></el-icon>
              </div>
              <div class="finance-content">
                <div class="finance-label">可用积分</div>
                <div class="finance-value">{{ memberInfo.points }}</div>
                <div class="finance-action">
                  <el-button link type="primary" @click="handleExchange">兑换</el-button>
                  <el-button link type="warning" @click="handleAdjustPoints">调整</el-button>
                </div>
              </div>
            </div>
            <el-divider />
            <div class="finance-summary">
              <div class="summary-item">
                <span>累计消费</span>
                <span class="summary-value">¥{{ memberInfo.totalConsumption.toLocaleString() }}</span>
              </div>
              <div class="summary-item">
                <span>累计充值</span>
                <span class="summary-value">¥{{ memberInfo.totalRecharge.toLocaleString() }}</span>
              </div>
              <div class="summary-item">
                <span>累计积分</span>
                <span class="summary-value">{{ memberInfo.totalPoints.toLocaleString() }}</span>
              </div>
            </div>
          </div>
        </el-card>
      </el-col>

      <!-- 会员分析 -->
      <el-col :xs="24" :lg="12">
        <el-card>
          <template #header>
            <span>📊 会员分析</span>
          </template>
          <div class="analysis-container">
            <div class="analysis-item">
              <div class="analysis-label">
                <el-icon><TrendCharts /></el-icon>
                RFM评分
              </div>
              <div class="analysis-content">
                <el-tag type="danger" size="large">{{ rfmScore }}</el-tag>
                <span class="analysis-desc">{{ rfmLabel }}</span>
              </div>
            </div>
            <div class="analysis-item">
              <div class="analysis-label">
                <el-icon><Calendar /></el-icon>
                最近消费
              </div>
              <div class="analysis-content">
                <span class="time-value">{{ daysSinceLastVisit }}天前</span>
                <span class="analysis-desc">{{ memberInfo.lastVisit }}</span>
              </div>
            </div>
            <div class="analysis-item">
              <div class="analysis-label">
                <el-icon><DataLine /></el-icon>
                消费频率
              </div>
              <div class="analysis-content">
                <span class="frequency-value">{{ frequency }}次/月</span>
                <el-rate v-model="activityLevel" disabled />
              </div>
            </div>
            <div class="analysis-item">
              <div class="analysis-label">
                <el-icon><Money /></el-icon>
                客单价
              </div>
              <div class="analysis-content">
                <span class="amount-value">¥{{ avgOrder }}</span>
                <el-progress 
                  :percentage="customerValuePercentage" 
                  :color="getValueColor(customerValuePercentage)"
                  :show-text="false"
                />
              </div>
            </div>
            <div class="analysis-item">
              <div class="analysis-label">
                <el-icon><Histogram /></el-icon>
                生命周期
              </div>
              <div class="analysis-content">
                <el-tag :type="getLifecycleTag(lifecycle)">{{ lifecycle }}</el-tag>
                <span class="analysis-desc">{{ lifecycleDesc }}</span>
              </div>
            </div>
            <div class="analysis-item">
              <div class="analysis-label">
                <el-icon><Warning /></el-icon>
                流失风险
              </div>
              <div class="analysis-content">
                <el-progress 
                  :percentage="churnRisk" 
                  :color="getChurnColor(churnRisk)"
                  type="dashboard"
                >
                  <template #default="{ percentage }">
                    <span :style="{ fontSize: '20px', color: getChurnColor(churnRisk) }">
                      {{ percentage }}%
                    </span>
                  </template>
                </el-progress>
              </div>
            </div>
          </div>
        </el-card>
      </el-col>
    </el-row>

    <!-- 最近活动 -->
    <el-row :gutter="20" style="margin-top: 20px;">
      <el-col :xs="24" :lg="16">
        <el-card>
          <template #header>
            <div class="card-header">
              <span>🛒 最近消费记录</span>
              <el-button size="small" @click="handleViewAllOrders">
                查看全部
              </el-button>
            </div>
          </template>
          <el-table :data="recentOrders" stripe>
            <el-table-column prop="orderNo" label="订单号" width="150" />
            <el-table-column prop="date" label="日期" width="160" />
            <el-table-column prop="products" label="商品" show-overflow-tooltip />
            <el-table-column prop="amount" label="金额" width="120">
              <template #default="{ row }">
                <span style="color: #f56c6c; font-weight: bold;">¥{{ row.amount.toLocaleString() }}</span>
              </template>
            </el-table-column>
            <el-table-column prop="points" label="获得积分" width="100">
              <template #default="{ row }">
                <el-tag type="warning" size="small">+{{ row.points }}</el-tag>
              </template>
            </el-table-column>
            <el-table-column prop="status" label="状态" width="100">
              <template #default="{ row }">
                <el-tag :type="getOrderStatusTag(row.status)" size="small">
                  {{ row.status }}
                </el-tag>
              </template>
            </el-table-column>
          </el-table>
        </el-card>
      </el-col>

      <!-- 积分动态 -->
      <el-col :xs="24" :lg="8">
        <el-card>
          <template #header>
            <div class="card-header">
              <span>⭐ 积分动态</span>
              <el-button size="small" @click="handleViewPoints">查看全部</el-button>
            </div>
          </template>
          <el-timeline class="points-timeline">
            <el-timeline-item
              v-for="record in pointsRecords"
              :key="record.id"
              :timestamp="record.date"
              :type="record.type === 'add' ? 'success' : 'warning'"
            >
              <div class="timeline-content">
                <div class="timeline-title">{{ record.title }}</div>
                <div class="timeline-points" :class="record.type">
                  {{ record.type === 'add' ? '+' : '-' }}{{ record.points }}积分
                </div>
              </div>
            </el-timeline-item>
          </el-timeline>
        </el-card>
      </el-col>
    </el-row>

    <!-- 优惠券和储值记录 -->
    <el-row :gutter="20" style="margin-top: 20px;">
      <el-col :xs="24" :lg="12">
        <el-card>
          <template #header>
            <div class="card-header">
              <span>🎫 优惠券</span>
              <el-button size="small" @click="handleSendCoupon">发放</el-button>
            </div>
          </template>
          <el-tabs v-model="couponTab">
            <el-tab-pane label="可用" name="available">
              <div class="coupon-list">
                <div v-for="coupon in availableCoupons" :key="coupon.id" class="coupon-item">
                  <div class="coupon-left">
                    <div class="coupon-value">
                      {{ coupon.type === 'cash' ? '¥' + coupon.value : coupon.value + '折' }}
                    </div>
                    <div class="coupon-condition">
                      {{ coupon.minAmount > 0 ? '满¥' + coupon.minAmount + '可用' : '无门槛' }}
                    </div>
                  </div>
                  <div class="coupon-right">
                    <div class="coupon-name">{{ coupon.name }}</div>
                    <div class="coupon-expire">有效期至: {{ coupon.expireDate }}</div>
                  </div>
                </div>
                <el-empty v-if="availableCoupons.length === 0" description="暂无可用优惠券" />
              </div>
            </el-tab-pane>
            <el-tab-pane label="已使用" name="used">
              <div class="coupon-list">
                <div v-for="coupon in usedCoupons" :key="coupon.id" class="coupon-item used">
                  <div class="coupon-left">
                    <div class="coupon-value">
                      {{ coupon.type === 'cash' ? '¥' + coupon.value : coupon.value + '折' }}
                    </div>
                  </div>
                  <div class="coupon-right">
                    <div class="coupon-name">{{ coupon.name }}</div>
                    <div class="coupon-used">使用时间: {{ coupon.usedDate }}</div>
                  </div>
                </div>
              </div>
            </el-tab-pane>
            <el-tab-pane label="已过期" name="expired">
              <div class="coupon-list">
                <el-empty description="暂无过期优惠券" />
              </div>
            </el-tab-pane>
          </el-tabs>
        </el-card>
      </el-col>

      <!-- 充值记录 -->
      <el-col :xs="24" :lg="12">
        <el-card>
          <template #header>
            <div class="card-header">
              <span>💳 充值记录</span>
              <el-button size="small" @click="handleRecharge">充值</el-button>
            </div>
          </template>
          <el-table :data="rechargeRecords" stripe max-height="300">
            <el-table-column prop="date" label="日期" width="160" />
            <el-table-column prop="amount" label="充值金额" width="110">
              <template #default="{ row }">
                ¥{{ row.amount.toFixed(2) }}
              </template>
            </el-table-column>
            <el-table-column prop="bonus" label="赠送" width="90">
              <template #default="{ row }">
                <span style="color: #67c23a;">+¥{{ row.bonus.toFixed(2) }}</span>
              </template>
            </el-table-column>
            <el-table-column prop="method" label="方式" width="100" />
            <el-table-column prop="operator" label="操作员" />
          </el-table>
        </el-card>
      </el-col>
    </el-row>

    <!-- 推荐策略 -->
    <el-card style="margin-top: 20px;">
      <template #header>
        <span>💡 智能推荐</span>
      </template>
      <el-row :gutter="20">
        <el-col :span="8">
          <div class="recommendation-card">
            <h4>🎯 营销建议</h4>
            <ul class="recommendation-list">
              <li v-for="(item, index) in marketingRecommendations" :key="index">
                {{ item }}
              </li>
            </ul>
          </div>
        </el-col>
        <el-col :span="8">
          <div class="recommendation-card">
            <h4>🛍️ 产品推荐</h4>
            <div class="product-recommendations">
              <div v-for="product in productRecommendations" :key="product.id" class="product-item">
                <img :src="product.image" v-if="product.image" alt="产品" />
                <div class="product-info">
                  <div class="product-name">{{ product.name }}</div>
                  <div class="product-price">¥{{ product.price }}</div>
                </div>
              </div>
            </div>
          </div>
        </el-col>
        <el-col :span="8">
          <div class="recommendation-card">
            <h4>🎁 优惠推荐</h4>
            <ul class="recommendation-list">
              <li v-for="(item, index) in offerRecommendations" :key="index">
                {{ item }}
              </li>
            </ul>
          </div>
        </el-col>
      </el-row>
    </el-card>

    <!-- 操作日志 -->
    <el-card style="margin-top: 20px;">
      <template #header>
        <div class="card-header">
          <span>📝 操作日志</span>
          <el-button size="small" @click="handleViewLogs">查看全部</el-button>
        </div>
      </template>
      <el-table :data="operationLogs" stripe>
        <el-table-column prop="time" label="时间" width="160" />
        <el-table-column prop="operator" label="操作员" width="120" />
        <el-table-column prop="action" label="操作" width="150" />
        <el-table-column prop="content" label="内容" />
        <el-table-column prop="ip" label="IP地址" width="150" />
      </el-table>
    </el-card>
  </div>
</template>

<script setup>
import { ref, reactive, computed, markRaw } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { 
  User, Phone, Message, Calendar, Clock, Wallet, Star, Ticket,
  ArrowDown, TrendCharts, DataLine, Money, Histogram, Warning,
  Select, ShoppingCart, Present, Trophy, Medal
} from '@element-plus/icons-vue'

const route = useRoute()
const router = useRouter()

// 会员信息
const memberInfo = reactive({
  id: 'M10001',
  name: '张三',
  phone: '13800138001',
  email: 'zhangsan@example.com',
  avatar: '',
  birthday: '1990-05-15',
  registerTime: '2023-01-15',
  level: 'diamond',
  growth: 8500,
  points: 8956,
  balance: 2500.00,
  totalConsumption: 125678,
  totalRecharge: 5000,
  totalPoints: 25678,
  lastVisit: '2024-01-20 14:30',
  tags: ['高价值客户', '活跃用户', '数码爱好者']
})

// KPI数据
const kpiData = ref([
  { 
    id: 1, 
    label: '累计消费', 
    value: '¥125,678', 
    desc: '近30天: ¥15,678',
    color: 'linear-gradient(135deg, #667eea 0%, #764ba2 100%)',
    icon: markRaw(ShoppingCart)
  },
  { 
    id: 2, 
    label: '订单数量', 
    value: '156', 
    desc: '近30天: 12单',
    color: 'linear-gradient(135deg, #f093fb 0%, #f5576c 100%)',
    icon: markRaw(DataLine)
  },
  { 
    id: 3, 
    label: '储值余额', 
    value: '¥2,500', 
    desc: '待到账: ¥0',
    color: 'linear-gradient(135deg, #4facfe 0%, #00f2fe 100%)',
    icon: markRaw(Wallet)
  },
  { 
    id: 4, 
    label: '可用积分', 
    value: '8,956', 
    desc: '即将过期: 0',
    color: 'linear-gradient(135deg, #43e97b 0%, #38f9d7 100%)',
    icon: markRaw(Star)
  }
])

// 成长数据
const nextLevelGrowth = ref(10000)
const nextLevelName = ref('至尊钻石')
const growthPercentage = computed(() => {
  return Math.floor((memberInfo.growth / nextLevelGrowth.value) * 100)
})

const customColors = [
  { color: '#f56c6c', percentage: 20 },
  { color: '#e6a23c', percentage: 40 },
  { color: '#5cb87a', percentage: 60 },
  { color: '#1989fa', percentage: 80 },
  { color: '#6f7ad3', percentage: 100 }
]

// 当前权益
const currentRights = ref([
  { id: 1, name: '8.5折优惠', icon: markRaw(Trophy), color: '#f56c6c' },
  { id: 2, name: '双倍积分', icon: markRaw(Star), color: '#e6a23c' },
  { id: 3, name: '免费配送', icon: markRaw(Present), color: '#67c23a' },
  { id: 4, name: '生日礼金¥200', icon: markRaw(Medal), color: '#409eff' },
  { id: 5, name: '专属客服', icon: markRaw(Phone), color: '#909399' },
  { id: 6, name: '优先发货', icon: markRaw(TrendCharts), color: '#67c23a' }
])

// 分析数据
const consumptionPeriod = ref('month')
const maxOrder = ref(12999)
const avgOrder = ref(806)
const frequency = ref(3.2)
const rfmScore = ref('VIP重要客户')
const rfmLabel = ref('高价值、高频次、近期活跃')
const daysSinceLastVisit = ref(5)
const activityLevel = ref(5)
const customerValuePercentage = ref(85)
const lifecycle = ref('成熟期')
const lifecycleDesc = ref('稳定消费，高忠诚度')
const churnRisk = ref(15)

// 最近订单
const recentOrders = ref([
  { orderNo: 'SO202401001', date: '2024-01-20 14:30', products: 'iPhone 15 Pro Max', amount: 9999, points: 999, status: '已完成' },
  { orderNo: 'SO202401002', date: '2024-01-18 10:20', products: 'AirPods Pro 2', amount: 1899, points: 189, status: '已完成' },
  { orderNo: 'SO202401003', date: '2024-01-15 16:45', products: 'iPad Air', amount: 4999, points: 499, status: '已完成' }
])

// 积分记录
const pointsRecords = ref([
  { id: 1, date: '2024-01-20 14:30', title: '消费获得', points: 999, type: 'add' },
  { id: 2, date: '2024-01-19 09:00', title: '每日签到', points: 5, type: 'add' },
  { id: 3, date: '2024-01-18 15:20', title: '积分兑换', points: 500, type: 'reduce' },
  { id: 4, date: '2024-01-18 10:20', title: '消费获得', points: 189, type: 'add' }
])

// 优惠券
const couponTab = ref('available')
const availableCoupons = ref([
  { id: 1, name: '50元代金券', type: 'cash', value: 50, minAmount: 200, expireDate: '2024-02-28' },
  { id: 2, name: '8折优惠券', type: 'discount', value: 8, minAmount: 0, expireDate: '2024-02-15' }
])

const usedCoupons = ref([
  { id: 1, name: '100元代金券', type: 'cash', value: 100, usedDate: '2024-01-20 14:30' }
])

// 充值记录
const rechargeRecords = ref([
  { date: '2024-01-10 15:30', amount: 1000, bonus: 150, method: '微信', operator: '收银员A' },
  { date: '2023-12-20 10:20', amount: 500, bonus: 50, method: '支付宝', operator: '收银员B' }
])

// 推荐数据
const marketingRecommendations = ref([
  '该会员为高价值客户，建议提供VIP专属优惠',
  '消费频次稳定，可推送会员日活动',
  '偏好数码产品，推荐新品iPhone 15',
  '生日即将到来，自动发送生日祝福和礼金'
])

const productRecommendations = ref([
  { id: 1, name: 'MacBook Pro 16', price: 19999, image: '' },
  { id: 2, name: 'Apple Watch Ultra', price: 6999, image: '' }
])

const offerRecommendations = ref([
  '充值2000送400活动即将结束',
  '钻石会员专享8.5折全场通用',
  '推荐好友注册可获200积分',
  '本月累计消费满5000送500积分'
])

// 操作日志
const operationLogs = ref([
  { time: '2024-01-20 14:30', operator: '收银员A', action: '会员消费', content: '消费¥9999，获得999积分', ip: '192.168.1.100' },
  { time: '2024-01-20 10:00', operator: '系统', action: '积分到账', content: '推荐好友注册，获得200积分', ip: 'System' },
  { time: '2024-01-19 09:00', operator: '会员', action: '每日签到', content: '签到获得5积分', ip: '192.168.1.101' }
])

// 辅助函数
const getLevelTag = (level) => {
  const map = { normal: 'info', silver: '', gold: 'warning', diamond: 'danger' }
  return map[level] || 'info'
}

const getLevelName = (level) => {
  const map = { normal: '普通会员', silver: '银卡会员', gold: '金卡会员', diamond: '钻石会员' }
  return map[level] || level
}

const getOrderStatusTag = (status) => {
  const map = { '已完成': 'success', '待付款': 'warning', '已取消': 'info' }
  return map[status] || 'info'
}

const getLifecycleTag = (lifecycle) => {
  const map = { '新客期': 'success', '成长期': 'primary', '成熟期': 'warning', '衰退期': 'danger' }
  return map[lifecycle] || 'info'
}

const getValueColor = (percentage) => {
  if (percentage >= 80) return '#67c23a'
  if (percentage >= 60) return '#409eff'
  if (percentage >= 40) return '#e6a23c'
  return '#f56c6c'
}

const getChurnColor = (risk) => {
  if (risk < 30) return '#67c23a'
  if (risk < 60) return '#e6a23c'
  return '#f56c6c'
}

// 操作函数
const handleRecharge = () => {
  ElMessage.info('会员充值')
}

const handleGivePoints = () => {
  ElMessage.info('赠送积分')
}

const handleSendCoupon = () => {
  ElMessage.info('发放优惠券')
}

const handleEdit = () => {
  ElMessage.info('编辑会员资料')
}

const handleUpgrade = () => {
  ElMessage.info('升级会员等级')
}

const handleAddTag = () => {
  ElMessage.info('添加会员标签')
}

const handleFreeze = () => {
  ElMessage.warning('冻结会员账户')
}

const handleViewGrowthRules = () => {
  ElMessage.info('查看成长规则')
}

const handleViewFinance = () => {
  ElMessage.info('查看详细账单')
}

const handleAdjustBalance = () => {
  ElMessage.info('调整储值余额')
}

const handleExchange = () => {
  router.push('/member/exchange')
}

const handleAdjustPoints = () => {
  ElMessage.info('调整积分')
}

const handleViewAllOrders = () => {
  ElMessage.info('查看全部订单')
}

const handleViewPoints = () => {
  ElMessage.info('查看全部积分记录')
}

const handleViewLogs = () => {
  ElMessage.info('查看全部操作日志')
}
</script>

<style scoped>
.member-dashboard {
  padding: 20px;
  background: #f0f2f5;
  min-height: 100vh;
}

/* 会员卡片 */
.member-card {
  margin-bottom: 20px;
}

.member-header {
  display: flex;
  gap: 30px;
  align-items: flex-start;
}

.member-avatar {
  position: relative;
}

.level-badge {
  position: absolute;
  bottom: -5px;
  left: 50%;
  transform: translateX(-50%);
}

.member-basic {
  flex: 1;
}

.member-name {
  margin: 0 0 15px 0;
  font-size: 24px;
  color: #303133;
}

.member-meta {
  display: flex;
  flex-wrap: wrap;
  gap: 20px;
  margin-bottom: 15px;
  color: #606266;
}

.meta-item {
  display: flex;
  align-items: center;
  gap: 5px;
}

.member-tags {
  margin-top: 15px;
}

.member-actions {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

/* KPI卡片 */
.kpi-row {
  margin-bottom: 20px;
}

.kpi-card {
  border: none;
  border-radius: 8px;
  transition: transform 0.3s;
}

.kpi-card:hover {
  transform: translateY(-5px);
}

.kpi-card :deep(.el-card__body) {
  display: flex;
  align-items: center;
  padding: 20px;
}

.kpi-icon {
  width: 60px;
  height: 60px;
  border-radius: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  margin-right: 15px;
}

.kpi-content {
  flex: 1;
}

.kpi-label {
  font-size: 14px;
  color: #909399;
  margin-bottom: 8px;
}

.kpi-value {
  font-size: 24px;
  font-weight: bold;
  color: #303133;
  margin-bottom: 5px;
}

.kpi-desc {
  font-size: 12px;
  color: #909399;
}

/* 卡片头部 */
.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

/* 成长信息 */
.growth-info {
  padding: 20px;
}

.current-level {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.level-label {
  font-size: 16px;
  font-weight: bold;
}

.growth-progress {
  margin-bottom: 20px;
}

.progress-header {
  display: flex;
  justify-content: space-between;
  margin-bottom: 10px;
  color: #606266;
}

.progress-value {
  font-weight: bold;
  color: #409eff;
}

.progress-tip {
  margin-top: 10px;
  color: #606266;
  font-size: 13px;
}

.highlight {
  color: #f56c6c;
  font-weight: bold;
}

.percentage-label {
  font-weight: bold;
}

.growth-actions {
  text-align: center;
}

.rights-info h4 {
  margin: 0 0 15px 0;
}

.right-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px;
  background: #f5f7fa;
  border-radius: 4px;
  margin-bottom: 10px;
}

.right-item span {
  flex: 1;
}

.check-icon {
  margin-left: auto;
}

/* 图表 */
.chart-container {
  height: 250px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.chart-placeholder {
  color: #909399;
  background: #fafafa;
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 4px;
}

.consumption-stats {
  margin-top: 20px;
  padding: 20px;
  background: #f5f7fa;
  border-radius: 4px;
}

.stat-item {
  text-align: center;
}

.stat-label {
  font-size: 13px;
  color: #909399;
  margin-bottom: 8px;
}

.stat-value {
  font-size: 20px;
  font-weight: bold;
  color: #303133;
}

/* 财务总览 */
.finance-overview {
  padding: 20px;
}

.finance-item {
  display: flex;
  align-items: center;
  gap: 20px;
}

.finance-icon {
  width: 60px;
  height: 60px;
  border-radius: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
}

.finance-icon.balance {
  background: linear-gradient(135deg, #67c23a 0%, #38f9d7 100%);
}

.finance-icon.points {
  background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
}

.finance-content {
  flex: 1;
}

.finance-label {
  font-size: 14px;
  color: #909399;
  margin-bottom: 8px;
}

.finance-value {
  font-size: 28px;
  font-weight: bold;
  color: #303133;
  margin-bottom: 8px;
}

.finance-action {
  display: flex;
  gap: 10px;
}

.finance-summary {
  margin-top: 20px;
}

.summary-item {
  display: flex;
  justify-content: space-between;
  padding: 12px 0;
  border-bottom: 1px dashed #ebeef5;
}

.summary-item:last-child {
  border-bottom: none;
}

.summary-value {
  font-weight: bold;
  color: #409eff;
}

/* 会员分析 */
.analysis-container {
  padding: 20px;
}

.analysis-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 15px;
  margin-bottom: 15px;
  background: #f5f7fa;
  border-radius: 8px;
}

.analysis-label {
  display: flex;
  align-items: center;
  gap: 8px;
  font-weight: bold;
  color: #606266;
  min-width: 120px;
}

.analysis-content {
  flex: 1;
  text-align: right;
}

.time-value,
.frequency-value,
.amount-value {
  font-size: 20px;
  font-weight: bold;
  color: #409eff;
  margin-right: 10px;
}

.analysis-desc {
  display: block;
  font-size: 12px;
  color: #909399;
  margin-top: 5px;
}

/* 积分时间轴 */
.points-timeline {
  max-height: 350px;
  overflow-y: auto;
  padding: 10px;
}

.timeline-content {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.timeline-title {
  color: #606266;
}

.timeline-points {
  font-weight: bold;
  font-size: 16px;
}

.timeline-points.add {
  color: #67c23a;
}

.timeline-points.reduce {
  color: #f56c6c;
}

/* 优惠券 */
.coupon-list {
  min-height: 200px;
  max-height: 300px;
  overflow-y: auto;
}

.coupon-item {
  display: flex;
  padding: 15px;
  margin-bottom: 10px;
  border: 2px dashed #409eff;
  border-radius: 8px;
  background: linear-gradient(135deg, #667eea15 0%, #764ba215 100%);
  transition: all 0.3s;
}

.coupon-item:hover {
  transform: translateX(5px);
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
}

.coupon-item.used {
  border-color: #dcdfe6;
  background: #f5f7fa;
  opacity: 0.7;
}

.coupon-left {
  width: 100px;
  text-align: center;
  border-right: 2px dashed #409eff;
  padding-right: 15px;
  margin-right: 15px;
}

.coupon-value {
  font-size: 24px;
  font-weight: bold;
  color: #f56c6c;
  margin-bottom: 5px;
}

.coupon-condition {
  font-size: 12px;
  color: #909399;
}

.coupon-right {
  flex: 1;
}

.coupon-name {
  font-weight: bold;
  color: #303133;
  margin-bottom: 5px;
}

.coupon-expire,
.coupon-used {
  font-size: 12px;
  color: #909399;
}

/* 推荐 */
.recommendation-card {
  padding: 20px;
  background: #f5f7fa;
  border-radius: 8px;
  height: 100%;
}

.recommendation-card h4 {
  margin: 0 0 15px 0;
  color: #303133;
}

.recommendation-list {
  list-style: none;
  padding: 0;
  margin: 0;
}

.recommendation-list li {
  padding: 10px 0;
  border-bottom: 1px dashed #e4e7ed;
  color: #606266;
  line-height: 1.6;
}

.recommendation-list li:last-child {
  border-bottom: none;
}

.recommendation-list li:before {
  content: '• ';
  color: #409eff;
  font-weight: bold;
  margin-right: 8px;
}

.product-recommendations {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.product-item {
  display: flex;
  gap: 10px;
  padding: 10px;
  background: white;
  border-radius: 4px;
}

.product-item img {
  width: 60px;
  height: 60px;
  object-fit: cover;
  border-radius: 4px;
}

.product-info {
  flex: 1;
}

.product-name {
  font-weight: bold;
  margin-bottom: 5px;
}

.product-price {
  color: #f56c6c;
  font-weight: bold;
}

/* 响应式 */
@media (max-width: 768px) {
  .member-header {
    flex-direction: column;
  }
  
  .member-actions {
    width: 100%;
  }
  
  .member-actions .el-button {
    width: 100%;
  }
}
</style>

